import com.dmotiko.general.*
import com.dmotiko.chabeli.*
import mx.utils.Delegate;

class ChabeliProducts
extends WebSiteSectionOLD {
	
	private var mcProductList:ChabeliProductList;
	private var mcProductList2:ChabeliProductList;
	private var mcProductList3:ChabeliProductList;
	private var mcProductList4:ChabeliProductList;
	private var mcShadowLeft:MovieClip;
	private var mcShadowRight:MovieClip;
	private var mcPopUp:PhotoPopUp;
	
	private function ChabeliProducts() {
		super(ChabeliSite.getApp());
		sName = ChabeliSite.PRODUCTOS;
		bShowed = false;
		Stage.addListener(this);
	}
	
	
	public function childLoaded( e ):Void {
		var aData:Array = new Array();
		var xmlData = ChabeliSite.getApp().getProductsData().total;
		if ( Stage.width > 1200 ) xmlData = xmlData.concat( com.dmotiko.util.ArrayUtil.clone(xmlData) );
		var nFraction:Number = Math.floor( xmlData.length / 4 );
		var nInit:Number = 0;
			
		switch( e.target ) {
			
			case mcProductList:
			var nLimit:Number = nFraction * 1 + 1;
			for (var i:Number = nInit; i < nLimit; i++) {
				aData.push( new ChabeliProductData(i, xmlData[i].name, xmlData[i].thumb, xmlData[i].big) );
			}
			mcProductList.setVelocity( 7 );
			mcProductList.setData( aData );
			mcProductList.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, showPhoto));
			break;
			
			case mcProductList2:
			var nLimit:Number = nFraction * 2 + 1;
			nInit = nFraction + 1;
			for (var i:Number = nInit; i < nLimit; i++) {
				aData.push( new ChabeliProductData(i, xmlData[i].name, xmlData[i].thumb, xmlData[i].big) );
			}
			mcProductList2.setVelocity( 5 );
			mcProductList2.setData( aData );
			mcProductList2.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, showPhoto));
			break;
			
			case mcProductList3:
			var nLimit:Number = nFraction * 3 + 1;
			nInit = nFraction * 2 + 1;
			for (var i:Number = nInit; i < nLimit; i++) {
				aData.push( new ChabeliProductData(i, xmlData[i].name, xmlData[i].thumb, xmlData[i].big) );
			}
			mcProductList3.setVelocity( 5 );
			mcProductList3.setData( aData );
			mcProductList3.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, showPhoto));
			break;
			
			case mcProductList4:
			var nLimit:Number = xmlData.length;
			nInit = nFraction * 3 + 1;
			for (var i:Number = nInit; i < xmlData.length; i++) {
				aData.push( new ChabeliProductData(i, xmlData[i].name, xmlData[i].thumb, xmlData[i].big) );
			}
			mcProductList4.setVelocity( 7 );
			mcProductList4.setData( aData );
			mcProductList4.addEventListener( BaseClipEvent.CHANGED, Delegate.create(this, showPhoto));
			break;
			
			case mcShadowRight:
			onResize();
			break;
		} 
		
		trace( "child loaded= " + e.target._name + "\ntotal= " + xmlData.length + "\ninit= "+nInit+"\nend= "+nLimit+"\n");
	}
	
	public function onResize():Void {
		mcShadowRight._x = Stage.width;
	}
	
	private function showPhoto(evnt){
		mcPopUp.setData( { photos: evnt.target.getData(), index: evnt.index } );
	}
	
}